package game;

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import heroes.Hero;

import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;

import cards.Card;

public class HeroChoosingDialog extends JDialog{
	View myView;
	boolean answerable = false;
	public static int EDGE;
	public static int HeroChoosingDialog_WIDTH;
	public static int HeroChoosingDialog_HEIGHT;
	
	public static void initPreferredSize() {
		EDGE = 5;
		//considering there will be two rows and 4 columns in the CardChoosingDialog
		HeroChoosingDialog_WIDTH = Hero.HERO_CARD_WIDTH*5+ EDGE*6;
		HeroChoosingDialog_HEIGHT = Hero.HERO_CARD_HEIGHT*1 + EDGE*7;
		System.out.println("HeroChoosingDialog_WIDTH "+ HeroChoosingDialog_WIDTH);
		System.out.println("HeroChoosingDialog_HEIGHT "+ HeroChoosingDialog_HEIGHT);
	}
	public HeroChoosingDialog(final View myView) {
		super(myView, "Choose Your Hero");
		//TODO is this going to do the fix?
//		setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		this.myView = myView;
		heroes = new ArrayList<Hero>();
		// maybe
		// new BoxLayout(this, BoxLayout.LINE_AXIS)
		setLayout(new FlowLayout());
		setPreferredSize(new Dimension(HeroChoosingDialog_WIDTH,HeroChoosingDialog_HEIGHT));
		this.setAlwaysOnTop(true);
		//@deprecated, see actionPerformed()
//		addMouseListener(new MouseAdapter() {
//			@Override
//			public void mouseClicked(MouseEvent e) {
//				System.out.println("Mouse click detected");
//				System.out.println("answerable: "+answerable);
//				System.out.println("Heroes:" + heroes);
//				System.out.println("e.getSource():"+e.getSource());
//				if (answerable) {
//					for (Hero every : heroes) {
//						if (e.getSource().equals(every)) {
//							
//							answerable = false;
//							myView.heroChosen(every);
//							break;
//						}
//					}
//				}
//			}
//		});
		this.pack();
		// @deprecated, see View.initHeroDialog()
		// answerable = true;
	}

	//TODO not initialized
	ArrayList<Hero> heroes;

	public void addHero(final Hero h) {
		heroes.add(h);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				add(h);
				pack();
//				revalidate();
				repaint();
			}
		});

	}
	
	public void actionPerformed(MouseEvent e) {
//		System.out.println("Mouse click detected");
//		System.out.println("answerable: "+answerable);
//		System.out.println("Heroes:" + heroes);
//		System.out.println("e.getSource():"+e.getSource());
		if (answerable) {
			for (Hero every : heroes) {
				if (e.getSource().equals(every)) {
					System.out.println("Hero found is:" + every);
					answerable = false;
					myView.heroChosen(every);
					break;
				}
			}
		}
	}
}
